<Project>
  <PropertyGroup>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <TestResultsName>testResults.xml</TestResultsName>
    <UseXunitExcludesTxtFile Condition="'$(TargetOS)' == 'android' or '$(TargetOS)' == 'ios' or '$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'tvossimulator' or '$(TargetOS)' == 'maccatalyst'">true</UseXunitExcludesTxtFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetsMobile)' != 'true' and '$(TestSingleFile)' != 'true'">
    <_depsFileArgument Condition="'$(GenerateDependencyFile)' == 'true'">--depsfile $(AssemblyName).deps.json</_depsFileArgument>
    <RunScriptCommand Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">"$(RunScriptHost)" exec --runtimeconfig $(AssemblyName).runtimeconfig.json $(_depsFileArgument) xunit.console.dll</RunScriptCommand>
    <RunScriptCommand Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">xunit.console.exe</RunScriptCommand>

    <RunScriptCommand>$(RunScriptCommand) $(TargetFileName)</RunScriptCommand>
    <RunScriptCommand>$(RunScriptCommand) -xml $(TestResultsName)</RunScriptCommand>
    <RunScriptCommand>$(RunScriptCommand) -nologo</RunScriptCommand>
    <RunScriptCommand Condition="'$(ArchiveTests)' == 'true'">$(RunScriptCommand) -nocolor</RunScriptCommand>
    <RunScriptCommand Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and '$(TestDisableAppDomain)' == 'true'">$(RunScriptCommand) -noappdomain</RunScriptCommand>
    <RunScriptCommand Condition="'$(TestDisableParallelization)' == 'true'">$(RunScriptCommand) -maxthreads 1</RunScriptCommand>
    <RunScriptCommand Condition="'$(XUnitShowProgress)' == 'true'">$(RunScriptCommand) -verbose</RunScriptCommand>
  </PropertyGroup>

  <PropertyGroup Condition="'$(UseXunitExcludesTxtFile)' != 'true'">
    <!-- Add local and global options to the argument stack. -->
    <RunScriptCommand Condition="'$(XUnitMethodName)' != ''">$(RunScriptCommand) -method $(XUnitMethodName)</RunScriptCommand>
    <RunScriptCommand Condition="'$(XUnitClassName)' != ''">$(RunScriptCommand) -class $(XUnitClassName)</RunScriptCommand>

    <!-- Add to run argument string -->
    <RunScriptCommand>$(RunScriptCommand)$(_withCategories.Replace(';', ' -trait category='))</RunScriptCommand>
    <RunScriptCommand>$(RunScriptCommand)$(_withoutCategories.Replace(';', ' -notrait category='))</RunScriptCommand>

    <!-- User passed in options. -->
    <RunScriptCommand Condition="'$(XUnitOptions)' != ''">$(RunScriptCommand) $(XUnitOptions)</RunScriptCommand>
  </PropertyGroup>

  <PropertyGroup Condition="'$(UseXunitExcludesTxtFile)' == 'true'">
    <XunitExcludesTxtFileContent>$(_withoutCategories.Replace(';', '%0dcategory='))</XunitExcludesTxtFileContent>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TestSingleFile)' == 'true'">
    <!-- Pass the results file -->
    <RunScriptCommand>$(RunScriptCommand) -xml $(TestResultsName)</RunScriptCommand>
  </PropertyGroup>

  <ItemGroup Condition="'$(TestSingleFile)' != 'true'">
    <PackageReference Include="Microsoft.DotNet.XUnitConsoleRunner"
                      Version="$(MicrosoftDotNetXUnitConsoleRunnerVersion)"
                      Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
    <PackageReference Include="xunit.runner.console"
                      Version="$(XUnitVersion)"
                      GeneratePathProperty="true"
                      Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'" />
  </ItemGroup>

  <!-- Overwrite the runner config file with the app local one. -->
  <Target Name="OverwriteDesktopTestRunnerConfigs"
          Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and
                     '$(GenerateAppConfigurationFile)' == 'true' and
                     '@(AppConfigWithTargetPath)' != ''"
          AfterTargets="CopyFilesToOutputDirectory">
    <ItemGroup>
      <_testRunnerConfigDestFile Include="$(TargetDir)xunit.console.exe.config" />
    </ItemGroup>

    <Copy SourceFiles="@(AppConfigWithTargetPath)"
          DestinationFiles="@(_testRunnerConfigDestFile)"
          SkipUnchangedFiles="true" />
  </Target>

  <!-- ResolveAssemblyReferences is the target that populates ReferenceCopyLocalPaths which is what is copied to output directory. -->
  <Target Name="CopyRunnerToOutputDirectory" BeforeTargets="ResolveAssemblyReferences" Condition="'$(TargetsMobile)' != 'true' or '$(BundleXunitRunner)' == 'true'">
    <ItemGroup>
      <!-- Copy test runner to output directory -->
      <None Include="$([System.IO.Path]::GetDirectoryName('$(XunitConsole472Path)'))\*"
            Exclude="$([System.IO.Path]::GetDirectoryName('$(XunitConsole472Path)'))\xunit.console.*exe.config;
                     $([System.IO.Path]::GetDirectoryName('$(XunitConsole472Path)'))\xunit.console.x86.exe;
                     $([System.IO.Path]::GetDirectoryName('$(XunitConsole472Path)'))\xunit.abstractions.dll"
            Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and '$(XunitConsole472Path)' != ''"
            CopyToOutputDirectory="PreserveNewest"
            Visible="false" />

      <_xunitConsoleNetCoreExclude Condition="'$(GenerateDependencyFile)' != 'true' and '$(XunitConsoleNetCore21AppPath)' != ''" Include="$([System.IO.Path]::GetDirectoryName('$(XunitConsoleNetCore21AppPath)'))\xunit.console.deps.json" />
      <None Include="$([System.IO.Path]::GetDirectoryName('$(XunitConsoleNetCore21AppPath)'))\*"
            Exclude="@(_xunitConsoleNetCoreExclude)"
            Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and '$(XunitConsoleNetCore21AppPath)' != ''"
            CopyToOutputDirectory="PreserveNewest"
            Visible="false" />
    </ItemGroup>

    <!-- Workaround for https://github.com/xunit/xunit/issues/1651 -->
    <ItemGroup Condition="'$(ArchiveTests)' != 'true'">
      <None Remove="$(Pkgxunit_runner_visualstudio)\build\net452\xunit.runner.utility.net452.dll" />
      <None Remove="$(Pkgxunit_runner_visualstudio)\build\net452\xunit.runner.reporters.net452.dll" />
      <None Remove="$(Pkgxunit_runner_visualstudio)\build\netcoreapp2.1\xunit.runner.utility.netcoreapp10.dll" />
      <None Remove="$(Pkgxunit_runner_visualstudio)\build\netcoreapp2.1\xunit.runner.reporters.netcoreapp10.dll" />
    </ItemGroup>
  </Target>
</Project>
